package org.baixin.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import org.baixin.entity.base.IdEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.IndexColumn;

/**
 * 
 * 
 * 系统资源
 * 
 * @author slaton.wu@gmail.com
 * @version 1.0
 * @created 2011-6-11 上午09:10:28
 */
@Entity(name = "pm_res")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Resource extends IdEntity {

	/**
	 * 资源中文名称
	 */
	private String rescname;

	/**
	 * 资源英文名称
	 * 
	 */
	private String resename;

	/**
	 * 资源地址
	 */
	private String url;

	/**
	 * 资源状态
	 * 
	 */
	private String status;

	/**
	 * 资源所有操作
	 */
	private List<Operate> ops;

	/**
	 * 当前机构所属机构
	 * 
	 */
	private Resource parent;

	/**
	 * 当前机构子机构集合
	 */
	private List<Resource> children;

	/**
	 * @param obj
	 * @return
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		if (obj != null) {
			return ((Resource) obj).getId().equals(this.getId());
		}
		return false;
	}

	/**
	 * 返回 children 的值
	 * 
	 * @return children
	 */
	@OneToMany(fetch=FetchType.EAGER,mappedBy = "parent")
	@OrderBy("id")
	@Fetch(FetchMode.SUBSELECT)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Resource> getChildren() {
		return children;
	}

	/**
	 * 返回 ops 的值
	 * 
	 * @return ops
	 */
	@OneToMany(mappedBy = "res", cascade = { CascadeType.REFRESH}, fetch = FetchType.EAGER, targetEntity = Operate.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Operate> getOps() {
		return ops;
	}

	/**
	 * 返回 parent 的值
	 * 
	 * @return parent
	 */
	@ManyToOne
	@JoinColumn(name = "pid")
	public Resource getParent() {
		return parent;
	}

	/**
	 * 返回 rescname 的值
	 * 
	 * @return rescname
	 */

	public String getRescname() {
		return rescname;
	}

	/**
	 * 返回 resename 的值
	 * 
	 * @return resename
	 */

	public String getResename() {
		return resename;
	}

	/**
	 * 返回 status 的值
	 * 
	 * @return status
	 */

	public String getStatus() {
		return status;
	}

	/**
	 * 返回 url 的值
	 * 
	 * @return url
	 */

	public String getUrl() {
		return url;
	}

	/**
	 * @return
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		long code = this.getId();
		return (int) code;
	}

	/**
	 * 设置 children 的值
	 * 
	 * @param children
	 */

	public void setChildren(List<Resource> children) {
		this.children = children;
	}

	/**
	 * 设置 ops 的值
	 * 
	 * @param ops
	 */

	public void setOps(List<Operate> ops) {
		this.ops = ops;
	}

	/**
	 * 设置 parent 的值
	 * 
	 * @param parent
	 */

	public void setParent(Resource parent) {
		this.parent = parent;
	}

	/**
	 * 设置 rescname 的值
	 * 
	 * @param rescname
	 */

	public void setRescname(String rescname) {
		this.rescname = rescname;
	}

	/**
	 * 设置 resename 的值
	 * 
	 * @param resename
	 */

	public void setResename(String resename) {
		this.resename = resename;
	}

	/**
	 * 设置 status 的值
	 * 
	 * @param status
	 */

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * 设置 url 的值
	 * 
	 * @param url
	 */

	public void setUrl(String url) {
		this.url = url;
	}

}
